import { getSecond , uuid} from '../../utils/common'
import { Account , Token , WorkChain , WorkNormal , AuthorContact} from '../../models/'
import { CODE , TOKEN_TIME , PROGRESS_STATUS , WORK_CATEGORY , WORK_TYPE } from '../../config/config'

/**
 * @api {post} /work/getWorkType 获取作品类型
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getWorkType
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getWorkType'
 *
 * @apiSuccess {String}   type               作品创作性质 
 * @apiSuccess {String}   gategory           作品类型
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getWorkType
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */

export async function getWorkType (ctx) {
	ctx.body = CODE.success({
		type:WORK_TYPE,
		gategory:WORK_CATEGORY
	})
}

/**
 * @api {post} /work/getNormalTypeCount 获取正常的作品数量(type)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getNormalTypeCount
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getNormalTypeCount'
 *
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应type数量
 * @apiSuccess {String}   type            type
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getNormalTypeCount
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getNormalTypeCount(ctx) {
	const typeCount     = await WorkNormal.typeCount()
	ctx.body            = CODE.success({counts:typeCount})
}

/**
 * @api {post} /work/getNormalTypeCountByUid 获取正常的作品数量(用户/type)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getNormalTypeCountByUid
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getNormalTypeCountByUid'
 *
 * @apiParam {String}  token              用户token
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应type数量
 * @apiSuccess {String}   type            type
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getNormalTypeCountByUid
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getNormalTypeCountByUid(ctx) {
	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}
	const typeCount     = await WorkNormal.typeCount({uid})
	ctx.body            = CODE.success({counts:typeCount})
}

/**
 * @api {post} /work/getNormalGategoryCount 获取正常的作品数量(gategory)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getNormalGategoryCount
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getNormalGategoryCount'
 *
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应gategory数量
 * @apiSuccess {String}   gategory        gategory
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getNormalGategoryCount
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getNormalGategoryCount(ctx) {
	const gategoryCount = await WorkNormal.gategoryCount()
	ctx.body            = CODE.success({counts:gategoryCount})
}

/**
 * @api {post} /work/getNormalTypeGategoryByUid 获取正常的作品数量(用户/gategory)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getNormalTypeGategoryByUid
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getNormalTypeGategoryByUid'
 *
 * @apiParam {String}  token              用户token
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应gategory数量
 * @apiSuccess {String}   gategory            gategory
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getNormalTypeGategoryByUid
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getNormalTypeGategoryByUid(ctx) {
	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}
	const gategoryCount = await WorkNormal.gategoryCount({uid})
	ctx.body            = CODE.success({counts:gategoryCount})
}




/**
 * @api {post} /work/getChainTypeCount 获取区块链的作品数量(type)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getChainTypeCount
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getChainTypeCount'
 *
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应type数量
 * @apiSuccess {String}   type            type
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getChainTypeCount
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getChainTypeCount(ctx) {
	const typeCount     = await WorkChain.typeCount()
	ctx.body            = CODE.success({counts:typeCount})
}

/**
 * @api {post} /work/getChainTypeCountByUid 获取区块链的作品数量(用户/type)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getChainTypeCountByUid
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getChainTypeCountByUid'
 *
 * @apiParam {String}  token              用户token
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应type数量
 * @apiSuccess {String}   type            type
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getChainTypeCountByUid
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getChainTypeCountByUid(ctx) {
	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}
	const typeCount     = await WorkChain.typeCount({uid})
	ctx.body            = CODE.success({counts:typeCount})
}

/**
 * @api {post} /work/getChainGategoryCount 获取区块链的作品数量(gategory)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getChainGategoryCount
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getChainGategoryCount'
 *
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应gategory数量
 * @apiSuccess {String}   gategory        gategory
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getChainGategoryCount
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getChainGategoryCount(ctx) {
	const gategoryCount = await WorkChain.gategoryCount()
	ctx.body            = CODE.success({counts:gategoryCount})
}

/**
 * @api {post} /work/getChainTypeGategoryByUid 获取区块链的作品数量(用户/gategory)
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName getChainTypeGategoryByUid
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/getChainTypeGategoryByUid'
 *
 * @apiParam {String}  token              用户token
 * 
 * @apiSuccess {Array}    counts          数量数组
 * @apiSuccess {Object}   countObject     数量对象
 * @apiSuccess {String}   count           对应gategory数量
 * @apiSuccess {String}   gategory            gategory
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/getChainTypeGategoryByUid
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */
export async function getChainTypeGategoryByUid(ctx) {
	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}
	const gategoryCount = await WorkChain.gategoryCount({uid})
	ctx.body            = CODE.success({counts:gategoryCount})
}


/**
 * @api {post} /work/createNormalWork 创建正常作品
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName createNormalWork
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/createNormalWork'
 *
 * @apiParam {String}  token              用户token
 * @apiParam {String}   name            作品名称
 * @apiParam {String}   type            作品创作性质
 * @apiParam {String}   gategory        作品类型
 * @apiParam {String}   complete_time   作品完成时间
 * @apiParam {String}   complete_place  作品完成动点              
 * @apiParam {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiParam {String}   release_time    作品发布时间 如果未发布传0或者不传     
 * @apiParam {String}   release_place   作品发布地点     
 * @apiParam {String}   url             作品下载url地址
 * @apiParam {String}   describe        作品描述
 * @apiParam {String}   contact_id      作品关联作者信息id
 * @apiParam {String}   hash_code       作品唯一hash码/md5码
 * @apiParam {String}   remark          备注
 * @apiParam {String}   ext             拓展字段
 * 
 * @apiSuccess {String}   id              作品id
 * @apiSuccess {String}   uid             创建作品用户id 
 * @apiSuccess {String}   name            作品名称
 * @apiSuccess {String}   type            作品创作性质
 * @apiSuccess {String}   gategory        作品类型
 * @apiSuccess {String}   complete_time   作品完成时间
 * @apiSuccess {String}   complete_place  作品完成动点             
 * @apiSuccess {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiSuccess {String}   release_time    作品发布时间 如果未发布传0或者不传 
 * @apiSuccess {String}   release_place   作品发布地点      
 * @apiSuccess {String}   url             作品下载url地址
 * @apiSuccess {String}   describe        作品描述
 * @apiSuccess {String}   contact_id      作品关联作者信息id
 * @apiSuccess {String}   hash_code       作品唯一hash码/md5码
 * @apiSuccess {String}   handle_code     处理码
 * @apiSuccess {String}   status          作品状态
 * @apiSuccess {String}   remark          备注
 * @apiSuccess {String}   ext             拓展字段
 * @apiSuccess {String}   ctime           创建时间
 * @apiSuccess {String}   htime           处理时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/createNormalWork
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */

export async function createNormalWork (ctx) {

	const token               = ctx.body.token
	const uid                 = token.phone_uid || token.wechat_uid || token.qq_uid
	

	const name                = ctx.request.body.name
	const url                 = ctx.request.body.url
	const describe            = ctx.request.body.describe
	
	const hash_code           = ctx.request.body.hash_code
	const remark              = ctx.request.body.remark
	

	const type                = ctx.request.body.type
	const gategory            = ctx.request.body.gategory
	const complete_time       = ctx.request.body.complete_time
	const complete_place      = ctx.request.body.complete_place
	const release_flag        = ctx.request.body.release_flag
	const release_time        = ctx.request.body.release_time
	const release_place       = ctx.request.body.release_place
	const ext                 = ctx.request.body.ext

	const contact_id          = ctx.request.body.contact_id
	const handle_code         = uuid()
	const ctime               = getSecond()

	// const oldWork             = await WorkNormal.findOne({where:hash_code})
	// if(oldWork) {
	// 	ctx.body              = CODE.ERROR.ERROR_WORK_ALREADY
	// 	return
	// }

	const work  = {
		uid,name,url,describe,hash_code,remark,type,gategory,complete_time,complete_place,release_flag,release_time,release_place,contact_id,handle_code,ctime,ext
	}

	const workNormal          = await WorkNormal.create(work)
	ctx.body                  = CODE.success({work:workNormal})
}


/**
 * @api {post} /work/cancelNormalWork 取消正常作品审核
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName cancelNormalWork
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/cancelNormalWork'
 *
 * @apiParam {String}  token            用户token
 * @apiParam {String}   id              作品id
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/cancelNormalWork
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */


export async function cancelNormalWork (ctx) {
	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}

	const workNormal       = await WorkNormal.findOne({where:{uid,id}})
	if(!workNormal) {
		ctx.body              = CODE.ERROR.ERROR_WORK_NOT_FOUND
		return
	}
	proveCompany.status      = PROGRESS_STATUS.CANCEL
	proveCompany.handle_code = uuid()
	await proveCompany.save()
	ctx.body                  = CODE.success()
}


/**
 * @api {post} /work/deleteNormalWork 删除正常作品审核
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName deleteNormalWork
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/deleteNormalWork'
 *
 * @apiParam {String}  token            用户token
 * @apiParam {String}   id              作品id
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/deleteNormalWork
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */


export async function deleteNormalWork (ctx) {
	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}

	const workNormal       = await WorkNormal.findOne({where:{uid,id}})
	if(!workNormal) {
		ctx.body              = CODE.ERROR.ERROR_WORK_NOT_FOUND
		return
	}
	proveCompany.status      = PROGRESS_STATUS.DELETE
	proveCompany.handle_code = uuid()
	await proveCompany.save()
	ctx.body                  = CODE.success()
}


/**
 * @api {post} /work/updateNormalWork 修改正常作品信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName updateNormalWork
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/updateNormalWork'
 *
 * @apiParam {String}  token              用户token
 * @apiParam {String}   id               作品id
 * @apiParam {String}   name            作品名称
 * @apiParam {String}   type            作品创作性质
 * @apiParam {String}   gategory        作品类型
 * @apiParam {String}   complete_time   作品完成时间
 * @apiParam {String}   complete_place  作品完成动点              
 * @apiParam {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiParam {String}   release_time    作品发布时间 如果未发布传0或者不传     
 * @apiParam {String}   url             作品下载url地址
 * @apiParam {String}   describe        作品描述
 * @apiParam {String}   contact_id      作品关联作者信息id
 * @apiParam {String}   hash_code       作品唯一hash码/md5码
 * @apiParam {String}   remark          备注
 * @apiParam {String}   ext             拓展字段
 * 
 * @apiSuccess {String}   id              作品id
 * @apiSuccess {String}   uid             创建作品用户id 
 * @apiSuccess {String}   name            作品名称
 * @apiSuccess {String}   type            作品创作性质
 * @apiSuccess {String}   gategory        作品类型
 * @apiSuccess {String}   complete_time   作品完成时间
 * @apiSuccess {String}   complete_place  作品完成动点             
 * @apiSuccess {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiSuccess {String}   release_time    作品发布时间 如果未发布传0或者不传     
 * @apiSuccess {String}   url             作品下载url地址
 * @apiSuccess {String}   describe        作品描述
 * @apiSuccess {String}   contact_id      作品关联作者信息id
 * @apiSuccess {String}   hash_code       作品唯一hash码/md5码
 * @apiSuccess {String}   handle_code     处理码
 * @apiSuccess {String}   status          作品状态
 * @apiSuccess {String}   remark          备注
 * @apiSuccess {String}   ext             拓展字段
 * @apiSuccess {String}   ctime           创建时间
 * @apiSuccess {String}   htime           处理时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/updateNormalWork
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */

export async function updateNormalWork (ctx) {

	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}

	const workNormal       = await WorkNormal.findOne({where:{uid,id}})
	if(!workNormal) {
		ctx.body              = CODE.ERROR.ERROR_WORK_NOT_FOUND
		return
	}
	
	const name = ctx.request.body.name
	const url = ctx.request.body.url
	const describe = ctx.request.body.describe
	
	const hash_code = ctx.request.body.hash_code
	const remark = ctx.request.body.remark
	

	const type = ctx.request.body.type
	const gategory = ctx.request.body.gategory
	const complete_time = ctx.request.body.complete_time
	const complete_place = ctx.request.body.complete_place
	const release_flag = ctx.request.body.release_flag
	const release_time = ctx.request.body.release_time
	const release_place = ctx.request.body.release_place
	const ext = ctx.request.body.ext

	const handle_code = uuid()

	workNormal.name           = name || workNormal.name
	workNormal.url            = url || workNormal.url
	workNormal.describe       = describe || workNormal.describe
	workNormal.hash_code      = hash_code || workNormal.hash_code
	workNormal.remark         = remark || workNormal.remark
	workNormal.type           = type || workNormal.type
	workNormal.gategory       = gategory || workNormal.gategory
	workNormal.complete_time  = complete_time || workNormal.complete_time
	workNormal.complete_place = complete_place || workNormal.complete_place
	workNormal.release_flag   = release_flag || workNormal.release_flag
	workNormal.release_time   = release_time || workNormal.release_time
	workNormal.release_place  = release_place || workNormal.release_place
	workNormal.ext  	      = ext || workNormal.ext
	workNormal.handle_code    = handle_code
	workNormal.status         = PROGRESS_STATUS.APPLYING

	await workNormal.save()
	ctx.body            = CODE.success({work:workNormal})
}


/**
 * @api {post} /work/normalWorkList 获取正常作品信息列表
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName normalWorkList
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/normalWorkList'
 *
 * @apiParam {String}  token              用户token
 * 
 * @apiSuccess {Array}   works         正常作品集合
 * @apiSuccess {String}   id              作品id
 * @apiSuccess {String}   uid             创建作品用户id 
 * @apiSuccess {String}   name            作品名称
 * @apiSuccess {String}   type            作品创作性质
 * @apiSuccess {String}   gategory        作品类型
 * @apiSuccess {String}   complete_time   作品完成时间
 * @apiSuccess {String}   complete_place  作品完成动点             
 * @apiSuccess {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiSuccess {String}   release_time    作品发布时间 如果未发布传0或者不传     
 * @apiSuccess {String}   url             作品下载url地址
 * @apiSuccess {String}   describe        作品描述
 * @apiSuccess {String}   contact_id      作品关联作者信息id
 * @apiSuccess {String}   hash_code       作品唯一hash码/md5码
 * @apiSuccess {String}   handle_code     处理码
 * @apiSuccess {String}   status          作品状态
 * @apiSuccess {String}   remark          备注
 * @apiSuccess {String}   ext             拓展字段
 * @apiSuccess {String}   ctime           创建时间
 * @apiSuccess {String}   htime           处理时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/normalWorkList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */

export async function normalWorkList (ctx) {

	const token               = ctx.body.token

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}

	const workNormals         = await WorkNormal.findAll({where:{uid,status:{$not:PROGRESS_STATUS.DELETE}}})
	const count               = await WorkNormal.count({where:{uid,status:{$not:PROGRESS_STATUS.DELETE}}})
	
	ctx.body            = CODE.success({works:workNormals,count})
}


/**
 * @api {post} /work/chainWorkList 获取区块链作品信息列表
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName chainWorkList
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/chainWorkList'
 *
 * @apiParam {String}  token              用户token
 * 
 * @apiSuccess {Array}   works         正常作品集合
 * @apiSuccess {String}   id              作品id
 * @apiSuccess {String}   uid             创建作品用户id 
 * @apiSuccess {String}   name            作品名称
 * @apiSuccess {String}   type            作品创作性质
 * @apiSuccess {String}   gategory        作品类型
 * @apiSuccess {String}   complete_time   作品完成时间
 * @apiSuccess {String}   complete_place  作品完成动点             
 * @apiSuccess {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiSuccess {String}   release_time    作品发布时间 如果未发布传0或者不传     
 * @apiSuccess {String}   url             作品下载url地址
 * @apiSuccess {String}   describe        作品描述
 * @apiSuccess {String}   contact_id      作品关联作者信息id
 * @apiSuccess {String}   hash_code       作品唯一hash码/md5码
 * @apiSuccess {String}   handle_code     处理码
 * @apiSuccess {String}   status          作品状态
 * @apiSuccess {String}   remark          备注
 * @apiSuccess {String}   ext             拓展字段
 * @apiSuccess {String}   ctime           创建时间
 * @apiSuccess {String}   htime           处理时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/chainWorkList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */

export async function chainWorkList (ctx) {

	const token               = ctx.body.token

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}

	const workChains          = await WorkChain.findAll({where:{uid,status:{$not:PROGRESS_STATUS.DELETE}}})
	const count               = await WorkChain.count({where:{uid,status:{$not:PROGRESS_STATUS.DELETE}}})
	
	ctx.body                  = CODE.success({works:workChains,count})
}


/**
 * @api {post} /work/createChainWork 创建区块链作品
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName createChainWork
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/createChainWork'
 *
 * @apiParam {String}  token              用户token
 * @apiParam {String}   name            作品名称
 * @apiParam {String}   type            作品创作性质
 * @apiParam {String}   gategory        作品类型
 * @apiParam {String}   complete_time   作品完成时间
 * @apiParam {String}   complete_place  作品完成动点              
 * @apiParam {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiParam {String}   release_time    作品发布时间 如果未发布传0或者不传 
 * @apiParam {String}   release_place   作品发布地点       
 * @apiParam {String}   url             作品下载url地址
 * @apiParam {String}   describe        作品描述
 * @apiParam {String}   contact_id      作品关联作者信息id
 * @apiParam {String}   hash_code       作品唯一hash码/md5码
 * @apiParam {String}   remark          备注
 * @apiParam {String}   ext             拓展字段
 * 
 * @apiSuccess {Object}   id              作品id
 * @apiSuccess {String}   uid             创建作品用户id 
 * @apiSuccess {String}   name            作品名称
 * @apiSuccess {String}   type            作品创作性质
 * @apiSuccess {String}   gategory        作品类型
 * @apiSuccess {String}   complete_time   作品完成时间
 * @apiSuccess {String}   complete_place  作品完成动点             
 * @apiSuccess {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiSuccess {String}   release_time    作品发布时间 如果未发布传0或者不传  
 * @apiSuccess {String}   release_place   作品发布地点        
 * @apiSuccess {String}   url             作品下载url地址
 * @apiSuccess {String}   describe        作品描述
 * @apiSuccess {String}   contact_id      作品关联作者信息id
 * @apiSuccess {String}   hash_code       作品唯一hash码/md5码
 * @apiSuccess {String}   handle_code     处理码
 * @apiSuccess {String}   status          作品状态
 * @apiSuccess {String}   remark          备注
 * @apiSuccess {String}   ext             拓展字段
 * @apiSuccess {String}   ctime           创建时间
 * @apiSuccess {String}   htime           处理时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/createChainWork
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */

export async function createChainWork (ctx) {

	const token               = ctx.body.token
	const uid                 = token.phone_uid || token.wechat_uid || token.qq_uid
	

	const name = ctx.request.body.name
	const url = ctx.request.body.url
	const describe = ctx.request.body.describe
	
	const hash_code = ctx.request.body.hash_code
	const remark = ctx.request.body.remark
	

	const type = ctx.request.body.type
	const gategory = ctx.request.body.gategory
	const complete_time = ctx.request.body.complete_time
	const complete_place = ctx.request.body.complete_place
	const release_flag = ctx.request.body.release_flag
	const release_time = ctx.request.body.release_time
	const release_place = ctx.request.body.release_place
	const ext = ctx.request.body.ext

	const contact_id = ctx.request.body.contact_id

	const handle_code = uuid()
	const ctime = getSecond()

	// const oldWork = await ChainWork.findOne({where:hash_code})
	// if(oldWork) {
	// 	ctx.body  = CODE.ERROR.ERROR_WORK_ALREADY
	// 	return
	// }
	
	const work  = {
		uid,name,url,describe,hash_code,remark,type,gategory,complete_time,complete_place,release_flag,release_time,release_place,contact_id,handle_code,ctime,ext
	}

	const workNormal     = await ChainWork.create(work)
	ctx.body            = CODE.success({work:workNormal})
}

/**
 * @api {post} /work/deleteChainWork 删除区块链作品审核
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName deleteChainWork
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/deleteChainWork'
 *
 * @apiParam {String}  token            用户token
 * @apiParam {String}   id              作品id
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/deleteChainWork
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */


export async function deleteChainWork (ctx) {
	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}

	const workChain           = await ChainWork.findOne({where:{uid,id}})
	if(!workChain) {
		ctx.body              = CODE.ERROR.ERROR_WORK_NOT_FOUND
		return
	}
	workChain.status          = 2
	proveCompany.handle_code  = uuid()
	await proveCompany.save()
	ctx.body                  = CODE.success()
}



/**
 * @api {post} /work/updateChainWork 修改区块链作品信息
 * @apiPermission 用户
 * @apiVersion 1.0.0 
 * @apiName updateChainWork
 * @apiGroup work
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/work/updateChainWork'
 *
 * @apiParam {String}  token              用户token
 * @apiParam {String}   id               作品id
 * @apiParam {String}   name            作品名称
 * @apiParam {String}   type            作品创作性质
 * @apiParam {String}   gategory        作品类型
 * @apiParam {String}   complete_time   作品完成时间
 * @apiParam {String}   complete_place  作品完成动点              
 * @apiParam {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiParam {String}   release_time    作品发布时间 如果未发布传0或者不传     
 * @apiParam {String}   url             作品下载url地址
 * @apiParam {String}   describe        作品描述
 * @apiParam {String}   contact_id      作品关联作者信息id
 * @apiParam {String}   hash_code       作品唯一hash码/md5码
 * @apiParam {String}   remark          备注
 * @apiParam {String}   ext             拓展字段
 * 
 * @apiSuccess {Object}   id              作品id
 * @apiSuccess {String}   uid             创建作品用户id 
 * @apiSuccess {String}   name            作品名称
 * @apiSuccess {String}   type            作品创作性质
 * @apiSuccess {String}   gategory        作品类型
 * @apiSuccess {String}   complete_time   作品完成时间
 * @apiSuccess {String}   complete_place  作品完成动点             
 * @apiSuccess {String}   release_flag    作品是否发布 1发布 2未发布           
 * @apiSuccess {String}   release_time    作品发布时间 如果未发布传0或者不传     
 * @apiSuccess {String}   url             作品下载url地址
 * @apiSuccess {String}   describe        作品描述
 * @apiSuccess {String}   contact_id      作品关联作者信息id
 * @apiSuccess {String}   hash_code       作品唯一hash码/md5码
 * @apiSuccess {String}   handle_code     处理码
 * @apiSuccess {String}   status          作品状态
 * @apiSuccess {String}   remark          备注
 * @apiSuccess {String}   ext             拓展字段
 * @apiSuccess {String}   ctime           创建时间
 * @apiSuccess {String}   htime           处理时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/work/updateChainWork
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 * 
 */

export async function updateChainWork (ctx) {

	const token               = ctx.body.token
	const id                  = ctx.request.body.id

	const phone_uid           = token.phone_uid
	const wechat_uid          = token.wechat_uid
	const qq_uid              = token.qq_uid

    const uid                 = []

	if(phone_uid) {
		uid[uid.length]       = phone_uid
	}
	if(wechat_uid) {
		uid[uid.length]       = wechat_uid
	}
	if(qq_uid) {
		uid[uid.length]       = qq_uid
	}

	const workChain           = await ChainNormal.findOne({where:{uid,id}})
	if(!workChain) {
		ctx.body              = CODE.ERROR.ERROR_WORK_NOT_FOUND
		return
	}
	
	const name = ctx.request.body.name
	const url = ctx.request.body.url
	const describe = ctx.request.body.describe
	
	const hash_code = ctx.request.body.hash_code
	const remark = ctx.request.body.remark
	

	const type = ctx.request.body.type
	const gategory = ctx.request.body.gategory
	const complete_time = ctx.request.body.complete_time
	const complete_place = ctx.request.body.complete_place
	const release_flag = ctx.request.body.release_flag
	const release_time = ctx.request.body.release_time
	const release_place = ctx.request.body.release_place
	const ext = ctx.request.body.ext

	const handle_code = uuid()

	workChain.name           = name || workNormal.name
	workChain.url            = url || workNormal.url
	workChain.describe       = describe || workNormal.describe
	workChain.hash_code      = hash_code || workNormal.hash_code
	workChain.remark         = remark || workNormal.remark
	workChain.type           = type || workNormal.type
	workChain.gategory       = gategory || workNormal.gategory
	workChain.complete_time  = complete_time || workNormal.complete_time
	workChain.complete_place = complete_place || workNormal.complete_place
	workChain.release_flag   = release_flag || workNormal.release_flag
	workChain.release_time   = release_time || workNormal.release_time
	workChain.release_place  = release_place || workNormal.release_place
	workChain.ext  			 = ext || workNormal.ext
	workChain.handle_code    = handle_code
	
	await workChain.save()
	ctx.body            = CODE.success({work:workChain})
}

export async function  checkToken(ctx,next) {
	const token 	  = ctx.request.body.token
	const tokenRes    = await Token.findOne({where : {token,status:1}})
	if(tokenRes) {
		if(tokenRes.ctime + TOKEN_TIME >= getSecond()) {
			ctx.body  = {token:tokenRes}
			if(next) {
				return next()
			}
		}
		ctx.body      = CODE.ERROR.ERROR_TOKEN_LOSE
		return
	}
	ctx.body          = CODE.ERROR.ERROR_TOKEN_NOT_CHECK
}



/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */